package com.order.controller.front_end;

import com.order.dao.Item;
import com.order.dao.Order;
import com.order.service.CartService;
import com.order.service.ItemService;
import com.order.service.OrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Scope("prototype")
@AllArgsConstructor
public class OrderController {

    private CartService cartService;
    private OrderService orderService;
    private ItemService itemService;

    /*用户下单*/
    @RequestMapping("/get_order/{desk_id}")
    public void getOrder(@PathVariable("desk_id") int desk_id, HttpServletResponse response) throws Exception {
        {
            response.setContentType("text/html;charset=UTF-8");
            int count =orderService.addOrder(desk_id);
            if (count>0) {
                response.getWriter().write("<script>alert('下单成功!');</script>");
            } else {
                response.getWriter().write("<script>alert('下单失败!');</script>");
            }
        }

    }

    /*订单详情*/
    @RequestMapping("/find_item/{desk_id}")
    public String findItem(@PathVariable("desk_id") int desk_id){
        Order order=orderService.getByDeskId(desk_id);
        List<Item> items = itemService.selectItem(order.getO_id());
        //    规范输出
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("订单如下：\n");
        for (Item i : items) {
            stringBuilder.append(i.toString()).append("\n");
        }
        log.info(stringBuilder.toString());
        return "操作成功";
    }
}
